﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using ArcEngine.Resources;
using System.ComponentModel;


namespace ArcEngine.GUI
{

	/// <summary>
	/// Base class for each GUI element
	/// </summary>
	public class GuiBase
	{

		public GuiBase(string name)
		{
			this.name = name;
		}

		
		#region Methods

		/// <summary>
		/// Draws the button
		/// </summary>
		public virtual void Draw()
		{
		}



		#endregion


		#region IO routines
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public virtual bool Save(XmlWriter xml)
		{
			if (xml == null)
				return false;


			xml.WriteStartElement("rectangle");
			xml.WriteAttributeString("x", Rectangle.X.ToString());
			xml.WriteAttributeString("y", Rectangle.Y.ToString());
			xml.WriteAttributeString("width", Rectangle.Width.ToString());
			xml.WriteAttributeString("height", Rectangle.Height.ToString());
			xml.WriteEndElement();


			xml.WriteStartElement("color");
			xml.WriteAttributeString("r", color.R.ToString());
			xml.WriteAttributeString("g", color.G.ToString());
			xml.WriteAttributeString("b", color.B.ToString());
			xml.WriteAttributeString("a", color.A.ToString());
			xml.WriteEndElement();

	

			xml.WriteStartElement("bgcolor");
			xml.WriteAttributeString("r", bgcolor.R.ToString());
			xml.WriteAttributeString("g", bgcolor.G.ToString());
			xml.WriteAttributeString("b", bgcolor.B.ToString());
			xml.WriteAttributeString("a", bgcolor.A.ToString());
			xml.WriteEndElement();

	
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		public virtual bool Load(XmlNode node)
		{
			if (node == null)
			return false;

			switch (node.Name.ToLower())
			{

				// Main color
				case "color":
				{
					color = Color.FromArgb(Int32.Parse(node.Attributes["a"].Value),
											Int32.Parse(node.Attributes["r"].Value),
											Int32.Parse(node.Attributes["g"].Value),
											Int32.Parse(node.Attributes["b"].Value));														
				}
				break;

				// Background color
				case "bgcolor":
				{
					bgcolor = Color.FromArgb(Int32.Parse(node.Attributes["a"].Value),
											Int32.Parse(node.Attributes["r"].Value),
											Int32.Parse(node.Attributes["g"].Value),
											Int32.Parse(node.Attributes["b"].Value));														
				}
				break;

	
				// Rectangle of the element
				case "rectangle":
				{

					Rectangle rect = Rectangle.Empty;
					rect.X = Int32.Parse(node.Attributes["x"].Value);
					rect.Y = Int32.Parse(node.Attributes["y"].Value);
					rect.Width = Int32.Parse(node.Attributes["width"].Value);
					rect.Height = Int32.Parse(node.Attributes["height"].Value);

					rectangle = rect;
				}
				break;


				// Unknown...
				default:
				{
					return false;
				}
			}

			return true;
		}

		#endregion


		#region Properties



		/// <summary>
		/// Element name
		/// </summary>
		[Browsable(false)]
		public string Name
		{
			get
			{
				return name;
			}
		}
		string name;


		/// <summary>
		/// Rectangle
		/// </summary>
		public Rectangle Rectangle
		{
			get
			{
				return rectangle;
			}
			set
			{
				rectangle = value;
			}
		}
		Rectangle rectangle;



		/// <summary>
		/// Size of the element
		/// </summary>
		[Browsable(false)]
		public Size Size
		{
			get
			{
				return Rectangle.Size;
			}
			set
			{
				rectangle.Size = value;
			}
		}



		/// <summary>
		/// Location of the element
		/// </summary>
		[Browsable(false)]
		public Point Location
		{
			get
			{
				return Rectangle.Location;
			}
			set
			{
				rectangle.Location = value;
			}
		}




		/// <summary>
		/// Gets/Sets the color of the element
		/// </summary>
		[Category("Color")]
		public Color Color
		{
			get
			{
				return color;
			}
			set
			{
				color = value;
			}
		}
		Color color = Color.White;
 
         
		/// <summary>
		/// Gets/Sets the background color of the element
		/// </summary>
		[Category("Color")]
		public Color BgColor
		{
			get
			{
				return bgcolor;
			}
			set
			{
				bgcolor = value;
			}
		}
		Color bgcolor = Color.Gray;
 
         
 
         

		#endregion
	}

}
